<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Contact_private extends wsFrontend
{
    public function index()
    {
        $data = array();
        $this->tpl->meta ['title'] = $this->metadata->get_metadata('_contact_page_title', 'contact');
        $this->tpl->meta ['description'] = $this->metadata->get_metadata('_contact_page_description', 'contact');
        $this->tpl->meta ['keywords'] = $this->metadata->get_metadata('_contact_page_keywords', 'contact');

        $this->load->library('form_validation');
        $data['captcha'] = false;
        if ($this->config->item('enable_captcha')) {
            $data['captcha'] = true;
        }
        $this->form_validation->set_rules('name', $this->language->get_text('name','global'), 'required');
        $this->form_validation->set_rules('email', $this->language->get_text('email','global'), 'required|valid_email');
        $this->form_validation->set_rules('subject', $this->language->get_text('subject','global'), 'required');
        $this->form_validation->set_rules('message', $this->language->get_text('message','global'), 'required|min_length[10]');
        if ($data['captcha'])
            $this->form_validation->set_rules('recaptcha_response_field', $this->language->get_text('captcha-code','global'), 'required');

        $data['error'] = $data['captchaError'] = false;
        if ($this->form_validation->run()) {
            $has_error = false;
            if ($data['captcha']) {
                $resp = recaptcha_check_answer('', $_SERVER["REMOTE_ADDR"], $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'));
                if (!$resp->is_valid) {
                    $data['captchaError'] = $resp->error;
                    $has_error = true;
                }
            }
            if (!$has_error) {
                $mail_data = array();
                $mail_data['subject'] = $this->input->post('subject');
                $mail_data['email'] = $this->input->post('email');
                $mail_data['name'] = $this->input->post('name');
                $mail_data['title'] = 'Contact #' . $mail_data['subject'] . ' - ' . $this->config->item('site_name');
                $mail_data['message'] = $this->input->post('message');
                $mail_body = $this->tpl->fetch('mail/contact_mail', $mail_data);


                $default_configs = array(
                    'mailtype' => 'html',
                    'charset' => 'utf-8'
                );

                $this->load->library('email');
                $this->email->initialize($default_configs);
                $this->email->from($mail_data['email'], $mail_data['name']);
                $this->email->to($this->config->item('contact_email'));
                $this->email->subject($mail_data['title']);
                $this->email->message($mail_body);
                $sent = $this->email->send();
                if ($sent) {
                    $this->session->set_flashdata('SUCCESS', $this->language->get_text('you-message-has-been-sent-successfully','comment'));
                    redirect(current_url());
                }else{
                    $data['error'] = $this->email->print_debugger();
                }
            }
        }

        $this->tpl->contents [] = $this->tpl->fetch('contents/contact_page', $data);
        $this->tpl->render('layouts/empty');
    }
}
